﻿using System;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using SysComm;
using SysOperationLayer.DBHandle;
using Resources;
using IPMS.Common;

public partial class OptAgents : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string strCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();

        if (Session["Culture"] != null)
        {
            strCulture = Session["Culture"].ToString();
        }

        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(strCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(strCulture);
        base.InitializeCulture();
    }
    
    private void GetAgentDeleteList(string agent, string chnName, string name, string contactPerson)
    {
        AgentsDB objDB = new AgentsDB();
        gvDeleted.DataSource = objDB.getAgentsList(agent, chnName, name, contactPerson, 1);
        gvDeleted.DataBind();
        objDB.CloseDB();
    }
    
    private void GetAgentActiveList(string agent, string chnName, string name, string contactPerson)
    {
        AgentsDB objDB = new AgentsDB();
        gvActive.DataSource = objDB.getAgentsList(agent, chnName, name, contactPerson, 0);
        gvActive.DataBind();
        objDB.CloseDB();
    }
    
    private void GetAgentList(string agent, string chnName, string name, string contactPerson)
    {
        GetAgentActiveList(agent, chnName, name, contactPerson);

        if (SysUtil.IsAdministratorUsertype())
        {
            GetAgentDeleteList(agent, chnName, name, contactPerson);
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else if (!IsPostBack)
        {
            ((Label)Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles.ToString() + " | " + ISitemap.AgentsList;
            panelDeleted.Visible = SysUtil.IsAdministratorUsertype();
            GetAgentList("", "", "", "");
        }
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetAgentList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
    }
    
    protected void btnNewAgent_Click(object sender, EventArgs e)
    {
        Response.Redirect("OptAgentsAdd.aspx");
    }
    
    protected void gvActive_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvActive.PageIndex = e.NewPageIndex;
        GetAgentActiveList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
    }
    
    protected void gvDeleted_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDeleted.PageIndex = e.NewPageIndex;
        GetAgentDeleteList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
    }
    
    protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandSource is ImageButton)
        {
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            GridView gv = (GridView)sender;
            AgentsDB objDB = new AgentsDB();
            string strAgent = gv.DataKeys[curRow.RowIndex].Values["Agent"].ToString();

            if (e.CommandName == "halfDelete")
            {
                if (objDB.Delete(strAgent, 
                                    Page.User.Identity.Name.ToString(),
                                    HttpContext.Current.Request.UserHostAddress) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_agent, "Delete Agent Master Success! Agent Code: " + strAgent);
                    Jscript.Alert("Delete Agent Master Success!", Page);
                    GetAgentList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_agent, "Delete Agent Master Failure! Agent Code: " + strAgent);
                    Jscript.Alert("Delete Agent Master Failure!", Page);
                }
            }
            else if (e.CommandName == "Restore")
            {
                if (objDB.Restore(strAgent) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_agent, "Restore Agent Master Success! Agent Code: " + strAgent);
                    Jscript.Alert("Restore Agent Master Success!", Page);
                    GetAgentList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_agent, "Restore Agent Master Failure! Agent Code: " + strAgent);
                    Jscript.Alert("Restore Agent Master Failure!", Page);
                }
            }
            else if (e.CommandName == "ForceDelete")
            {
                if (objDB.Trash(strAgent) == 0)
                {
                    new LogDB(objDB.GetDB()).writeSucLog(SysUtil.m_functiong_agent, "Trash Agent Master Success! Agent Code: " + strAgent);
                    Jscript.Alert("Trash Agent Master Success!", Page);
                    GetAgentList(txtSearchAgent.Text.Trim(), txtSearchChineseName.Text.Trim(), txtSearchName.Text.Trim(), txtSearchContactPerson.Text.Trim());
                }
                else
                {
                    new LogDB(objDB.GetDB()).writeFaileLog(SysUtil.m_functiong_agent, "Trash Agent Master Failure! Agent Code: " + strAgent);
                    Jscript.Alert("Trash Agent Master Failure!", Page);
                }
            }

            objDB.CloseDB();
        }
    }
    
    protected void gvActive_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }
}
